import fetch from './fetch';

export function sendCode(phone) {
  const params = {
    phone
  };
  return fetch({
    url: '/api/user/sendcode',
    method: 'post',
    params
  });
}

export function login(phone, code, channel) {
  const params = {
    phone,
    code,
    channel
  };
  return fetch({
    url: '/api/user/login',
    method: 'post',
    params
  });
}

export function logout() {
  return fetch({
    url: '/api/user/logout',
    method: 'post'
  });
}

export function getLoginStatus() {
  return fetch({
    url: '/api/user/loginstatus',
    method: 'post'
  });
}

export function getRecommendCourse() {
  const ts = (new Date()).getTime();
  const params = {
    ts
  }
  return fetch({
    url: '/api/course/recommend',
    method: 'get',
    params
  });
}

export function getIndexCourse(offset, limit) {
  const ts = (new Date()).getTime();
  const params = {
    offset,
    limit,
    ts
  }
  return fetch({
    url: '/api/course/index',
    method: 'get',
    params
  });
}

export function getMyCourse() {
  const ts = (new Date()).getTime();
  const params = {
    ts
  }
  return fetch({
    url: '/api/course/my',
    method: 'get',
    params
  });
}

export function getCourseDetail(courseId, randId) {
  const ts = (new Date()).getTime();
  const params = {
    courseId,
    randId,
    ts
  }
  return fetch({
    url: '/api/course/detail',
    method: 'get',
    params
  });
}

export function getCourseByCategory(categoryId, randId) {
  const ts = (new Date()).getTime();
  const params = {
    categoryId,
    randId,
    ts
  }
  return fetch({
    url: '/api/course/category',
    method: 'get',
    params
  });
}

export function getCourseAudios(courseId, randId, channel) {
  const ts = (new Date()).getTime();
  const params = {
    courseId,
    randId,
    channel,
    ts
  }
  return fetch({
    url: '/api/course/audios',
    method: 'get',
    params
  });
}

export function getAudioUrl(audioId, courseId, randId) {
  const ts = (new Date()).getTime();
  const params = {
    audioId,
    courseId,
    randId,
    ts
  }
  return fetch({
    url: '/api/audio/url',
    method: 'get',
    params
  });
}

export function getOrderSN(courseId) {
  const ts = (new Date()).getTime();
  const params = {
    courseId,
    ts
  }
  return fetch({
    url: '/api/order/sn',
    method: 'get',
    params
  });
}

export function getOrderStatus(orderSN) {
  const ts = (new Date()).getTime();
  const params = {
    orderSN,
    ts
  }
  return fetch({
    url: '/api/order/status',
    method: 'get',
    params
  });
}


export function createWxOrder(openId, courseId, orderSN, channel) {
  const ts = (new Date()).getTime();
  const params = {
    openId,
    courseId,
    orderSN,
    channel,
    ts
  }
  return fetch({
    url: '/api/wxorder/jsapi',
    method: 'get',
    params
  });
}

export function bind(orderSN, phone, code) {
  const params = {
    orderSN,
    phone,
    code
  }
  return fetch({
    url: '/api/order/bind',
    method: 'post',
    params
  });
}

export function getCourseInfoByOrderSN(orderSN) {
  const ts = (new Date()).getTime();
  const params = {
    orderSN,
    ts
  }
  return fetch({
    url: '/api/order/course',
    method: 'get',
    params
  });
}
